window.ROOT = "";
(function() {
	var serverData = {};
	var serverTable;
	jQuery(document)
			.ready(
					function() {
						serverTable = jQuery('#dataTable')
								.DataTable(
										{
											"serverSide" : true,
											"ordering" : false,
											"ajax" : {
												"dataType" : "json",
												"type" : "get",
												"cache" : false,
												"url" : window.rootPath
														+ '/ui/index/getUserInfoList/'
														+ $("#deptId").val(),
												"data" : function(d) {
													return $.extend({}, d,
															serverData);
												}
											},
											columns : [
													{
														data : null,
														defaultContent : ""
													},
													{
														data : 'id',
														render : function(data,
																type, full,
																meta) {
															var operate = '<input type="checkbox" class="addUser" dataId="'
																	+ data
																	+ '" group="addUser"/>';
															return operate;
														}
													},
													{
														data : 'name'
													},
													{
														data : 'orderNum'
													},
													{
														data : 'sex'
													},
													{
														data : 'contactType'
													},
													{
														data : 'email'
													},
													{
														data : 'post.title'
													},
													{
														data : 'memo'
													},
													{
														data : null,
														defaultContent : "",
														render : function(data,
																type, full,
																meta) {
															var operate = '<button type="button" class="btn btn-sm btn-info selectBtn" title="选择"><i class="fa fa-flag"></i></button>'
															return operate;

														}
													} ],
											"drawCallback" : function(oSettings) {
												var k = 0;
												var total = oSettings.aiDisplay.length
														+ oSettings._iDisplayStart;

												for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
													$(
															'td:eq(0)',
															oSettings.aoData[oSettings.aiDisplay[k]].nTr)
															.text(i + 1);
													k++;
												}

												serverTable.off('click', 'tr');
												serverTable
														.on(
																'click',
																'tr',
																function(event) {
																	event = event
																			|| window.event;
																	var target = event.target
																			|| event.scrElement;
																	var data = serverTable
																			.row(
																					this)
																			.data();

																	if (target.className
																			.indexOf('fa-flag') != -1
																			|| target.className
																					.indexOf('selectBtn') != -1) {
																		selectData(data);
																	}
																	return;
																});
											}
										})
					});
	var selectData = function(data) {
		if (!data)
			return;
		// $('#userList', parent.document).val(data.id);
		var ids=[];
		ids.push(data.id);
		window.parent.window.getUserToMapIds(ids);
	}
	jQuery('.togetherBtn').on('click', function() {
		var dataInfo = getSelectData('addUser');
    	if(dataInfo.length == 0){
    		message('请选择需要跟踪的人员!');
		return;
    	}
    	window.parent.window.getUserToMapIds(dataInfo);
	});
	 //获取全选数据
    function getSelectData(group){
    		var obj = $(":checkbox[group="+group+"]:checked");
    		var myArray = new Array();
    		var i =0;
    		obj.each(function() {
    			myArray[i++] = $(this).attr('dataId');
    		});
    		return myArray;
    }
	//全选-取消操作
	$("#allSelect").click(function() {
		var addUser = $(":checkbox[group=addUser]");
    if ($(this).prop("checked")) {
    	addUser.each(function() {
            $(this).prop("checked", true);  
        });  
    } else {  
    	addUser.each(function() {  
            $(this).prop("checked", false);  
        });  
    }  
}); 
	// 查询
	jQuery('.searchBtn').on('click', function() {
		serverData.key = jQuery("#search").val();
		serverTable.draw();
	});
	// 撤销查询
	jQuery('.resetBtn').on('click', function() {
		jQuery('#search').val('');
		serverData.key = '';
		serverTable.draw();
	});
})();